﻿using System;
using System.Collections;
using System.Collections.Generic;

using IBatisNet.DataAccess;

using AUV5.Model;
using AUV5.DAL;
using AUV5.DAL.Interface;
using AUV5.DAL.Dao;

namespace AUV5.BLL
{
    /// <summary>
    /// 功能：     ServiceKeyRela业务逻辑实现
    /// 作者：     lwl
    /// 日期：     2011/10/14
    /// 修改日期： 
    /// 修改人：
    /// 修改内容：
    /// </summary>
    public class ServiceKeyRelaBLL
    {
        private static ServiceKeyRelaBLL instance = null;
        private static object syncRoot = new object();
        private IDaoManager daoManager = ServiceConfig.Instance.DaoManager;
        private IServiceKeyRelaDao dao = null;

        private ServiceKeyRelaBLL()
        {
            dao = ServiceKeyRelaSqlMapDao.Instance;
        }

        public static ServiceKeyRelaBLL Instance
        {
            get
            {
                if (instance == null)
                {
                    lock (syncRoot)
                    {
                        if (instance == null)
                        {
                            instance = new ServiceKeyRelaBLL();
                        }
                    }
                }
                return instance;
            }
        }

        /// <summary>
        /// 得到最大行数
        /// </summary>
        /// <returns></returns>
        public int GetMaxCounts()
        {
            try
            {
                //daoManager.BeginTransaction();

                int count = dao.GetMaxCounts();

                //daoManager.CommitTransaction();

                return count;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 得到列表
        /// </summary>
        /// <returns></returns>
        public System.Collections.Generic.IList<ServiceKeyRela> GetList()
        {
            try
            {
                //daoManager.BeginTransaction();

                System.Collections.Generic.IList<ServiceKeyRela> list = dao.GetList();

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 通过服务信息的ID得到关键字列表
        /// </summary>
        /// <param name="TopCounts"></param>
        /// <returns></returns>
        public System.Collections.Generic.IList<ServiceKeyRela> GetKeysByServiceID(object TopCounts, string serviceID)
        {
            try
            {
                //daoManager.BeginTransaction();
                Hashtable ht = new Hashtable();
                ht.Add("TopCounts", TopCounts);
                ht.Add("serviceID", serviceID);
                ht.Add("state", 1); 
                //获取的是State=1的值
                System.Collections.Generic.IList<ServiceKeyRela> list = dao.GetListByParameters(ht);

                //daoManager.CommitTransaction();

                return list;
            }
            catch
            {
                //daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 得到明细
        /// </summary>
        /// <param name="RelaID"></param>
        /// <returns></returns>
        public ServiceKeyRela GetDetail(System.String relaid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.GetDetail(relaid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }


        /// <summary>
        /// 新建
        /// </summary>
        /// <param name="servicekeyrela"></param>
        /// <returns></returns>
        public object Add(ServiceKeyRela servicekeyrela)
        {
            //int id = GetId("SYS_UserInfoTemp");
            //sys_userinfotemp.UserID = id;

            try
            {
                ///daoManager.BeginTransaction();
                servicekeyrela.State = (int)Common.Enum.auvState.Enabled;
                servicekeyrela.IsDel = (int)Common.Enum.auvDel.NotDel;
                servicekeyrela.CreateDate = DateTime.Now;
                servicekeyrela.UpdateDate= DateTime.Now;
                servicekeyrela.BeginDate=  DateTime.Now;
                servicekeyrela.EndDate=  DateTime.Now;;
                return dao.Add(servicekeyrela);
                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 修改
        /// </summary>
        /// <param name="servicekeyrela"></param>
        /// <returns></returns>
        public int Update(ServiceKeyRela servicekeyrela)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Update(servicekeyrela);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="RelaID"></param>
        /// <returns></returns>
        public int Delete(System.String relaid)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(relaid);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }
        /// <summary>
        /// 删除
        /// </summary>
        /// <param name="servicekeyrela"></param>
        /// <returns></returns>
        public int Delete(ServiceKeyRela servicekeyrela)
        {
            try
            {
                ///daoManager.BeginTransaction();

                return dao.Delete(servicekeyrela);

                ///daoManager.CommitTransaction();
            }
            catch
            {
                ///daoManager.RollBackTransaction();
                throw;
            }
        }

        public void DisableList(Hashtable ht)
        {
            try
            {
                dao.Disable(ht);
            }
            catch 
            {
                
                throw;
            }
        }

        public string GetListByKeyList(string[] list,string exceptServiceID,int topCounts)
        {
            try
            {
                if (list.Length > 0)
                {
                    Hashtable ht = new Hashtable();
                    ht.Add("topCounts",topCounts);
                    ht.Add("list", list);
                    ht.Add("except",exceptServiceID);
                    ht.Add("state",Common.Enum.auvState.Enabled);
                    ht.Add("isActive", Common.Enum.ServiceStatus.Active);
                    ht.Add("isDel", Common.Enum.auvDel.NotDel);
                    IList<ServiceKeyRela> relalist=dao.GetListByParameters(ht);
                    string idlist = "";
                    for (int i = 0; i < relalist.Count; i++)
                    {
                        if (i == 0)
                        {
                            idlist = "'"+relalist[i].ServiceID+"'";
                        }
                        else
                        { 
                            idlist += "," + "'"+relalist[i].ServiceID+"'";
                        }
                    }
                    return idlist;
                }
                else
                {
                    return null;
                }
            }
            catch 
            {
                
                throw;
            }
            
        }
    }
}
